import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'

Vue.config.productionTip = false
Vue.use(ElementUI)

// 配置axios
axios.defaults.baseURL = process.env.VUE_APP_BASE_API || ''
axios.interceptors.response.use(
  response => response,
  error => {
    console.error('请求错误:', error)
    ElementUI.Message.error('网络请求失败，请稍后再试')
    return Promise.reject(error)
  }
)

// 路由守卫
router.beforeEach((to, from, next) => {
  const user = JSON.parse(localStorage.getItem('user') || '{}')
  
  if (to.meta.requiresAuth && !user.id) {
    next('/login')
  } else if (to.meta.requiresAdmin && !user.isAdmin) {
    next('/home')
  } else {
    next()
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app') 